package com.platform.qujia.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.platform.qujia.dao.*;
import com.platform.qujia.param.request.PayOrderRequest;
import com.platform.qujia.pojo.*;
import com.platform.qujia.pojo.vo.ExchangeCardVo;
import com.platform.qujia.service.*;
import org.apache.commons.lang3.StringUtils;
import com.platform.qujia.utils.exception.CustomException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service("electronicDeputyCardService")
@Slf4j
public class ElectronicDeputyCardServiceImpl implements ElectronicDeputyCardService {

    @Autowired
    private CustomerElectronicDeputyCardMapper electronicDeputyCardMapper;

    @Autowired
    private AppUserInfoService appUserInfoService;

    @Resource
    private ElectronicMembershipCardMapper electronicMembershipCardMapper;

    @Resource
    private DeputyCardRuleMapper deputyCardRuleMapper;

    @Resource
    private PayOrderMapper payOrderMapper;

    @Resource
    private OilMapper oilMapper;

    @Resource
    private EnterpriseService enterpriseService;

    @Resource
    private StationMapper stationMapper;

    @Autowired
    private ShareRecoveryRecordMapper shareRecoveryRecordMapper;



    @Override
    public List<CustomerElectronicDeputyCard> selectElectronicDeputyCardList(CustomerElectronicDeputyCard customerElectronicDeputyCard) {
        List<CustomerElectronicDeputyCard> deputyCardList = selectCustomerElectronicDeputyCardList(customerElectronicDeputyCard);
        for (CustomerElectronicDeputyCard electronicDeputyCard : deputyCardList) {
            CustomerElectronicCard req = new CustomerElectronicCard();
            req.setId(electronicDeputyCard.getMainId().intValue());
            CustomerElectronicCard electronicCardInfo = electronicMembershipCardMapper.getElectronicCardInfo(req);
            AppUserInfo appUserInfo = new AppUserInfo();
            if(Objects.nonNull(electronicCardInfo.getStationId())){
                appUserInfo.setStationId(electronicCardInfo.getStationId());
            } else if(Objects.nonNull(electronicCardInfo.getStationId2())){
                Station station = stationMapper.selectStationById(electronicCardInfo.getStationId2());
                appUserInfo.setStationId(station.getEnterpriseId());
            }
            appUserInfo.setMobilePhone(electronicCardInfo.getMobilePhone());
            AppUserInfo appUser = appUserInfoService.getAppUserInfo(appUserInfo);
            electronicDeputyCard.setCustomerName(appUser.getBlogNickName());
        }
        return deputyCardList;
    }

    @Override
    public List<CustomerElectronicDeputyCard> selectElectronicDeputyCardByMainList(CustomerElectronicDeputyCard customerElectronicDeputyCard) {
        AppUserInfo appUser = appUserInfoService.getAppUserById(customerElectronicDeputyCard.getUserId());
        log.info("请求用户：[{}]",JSONObject.toJSONString(appUser));
        List<Integer> mainIdList = new ArrayList<>();
        CustomerElectronicCard req = new CustomerElectronicCard();
        req.setMobilePhone(appUser.getMobilePhone());
        req.setStationId(appUser.getStationId());
        List<CustomerElectronicCard> electronicCardList = electronicMembershipCardMapper.getElectronicCardList(req);
        if(CollectionUtils.isEmpty(electronicCardList)){
            Station sta = new Station();
            sta.setEnterpriseId(appUser.getStationId());
            List<Station> stations = stationMapper.queryStationList(sta);
            CustomerElectronicCard card = new CustomerElectronicCard();
            card.setMobilePhone(appUser.getMobilePhone());
            List<Integer> stationIdList = stations.stream().map(Station::getStationId).collect(Collectors.toList());
            card.setStationIdList(stationIdList);
            List<CustomerElectronicCard> resultCardList = electronicMembershipCardMapper.getElectronicCardList(card);
            List<Integer> ids = resultCardList.stream().map(CustomerElectronicCard::getId).collect(Collectors.toList());
            mainIdList.addAll(ids);
        }else {
            List<Integer> ids = electronicCardList.stream().map(CustomerElectronicCard::getId).collect(Collectors.toList());
            mainIdList.addAll(ids);
        }
        if(CollectionUtils.isEmpty(mainIdList)){
            return null;
        }
        CustomerElectronicDeputyCard deputyCard = new CustomerElectronicDeputyCard();
        deputyCard.setMainIdList(mainIdList);
        log.info("请求主卡号：[{}]",JSONObject.toJSONString(deputyCard));
        return selectCustomerElectronicDeputyCardList(deputyCard);
    }

    // 查询副卡信息
    private List<CustomerElectronicDeputyCard> selectCustomerElectronicDeputyCardList(CustomerElectronicDeputyCard deputyCard){
        List<CustomerElectronicDeputyCard> customerElectronicDeputyCards = electronicDeputyCardMapper.selectElectronicDeputyCardList(deputyCard);
        if(CollectionUtils.isEmpty(customerElectronicDeputyCards)){
            return null;
        }
        for (CustomerElectronicDeputyCard electronicDeputyCard : customerElectronicDeputyCards) {
            DeputyCardRule rule = deputyCardRuleMapper.selectByRuleId(electronicDeputyCard.getRuleId());
            // 共享
            if(Objects.nonNull(rule)){
                if( "1".equals(rule.getCardRuleType())) {
                    CustomerElectronicCard customerElectronicCard = new CustomerElectronicCard();
                    customerElectronicCard.setId(electronicDeputyCard.getMainId().intValue());
                    CustomerElectronicCard electronicCardInfo = electronicMembershipCardMapper.getElectronicCardInfo(customerElectronicCard);
                    if (Objects.nonNull(electronicCardInfo)) {
                        electronicDeputyCard.setAmt((Objects.nonNull(electronicCardInfo.getAmt()) ? electronicCardInfo.getAmt() : BigDecimal.ZERO)
                                .add((Objects.nonNull(electronicCardInfo.getPresentAmt()) ? electronicCardInfo.getPresentAmt() : BigDecimal.ZERO)));
                    }
                }
                electronicDeputyCard.setCardRuleType(rule.getCardRuleType());
            }

            if(Objects.nonNull(electronicDeputyCard.getStationId2())){
                Station station = stationMapper.selectStationById(electronicDeputyCard.getStationId2());
                electronicDeputyCard.setStationName(station.getStationName());
            }
        }
        return customerElectronicDeputyCards;
    }


    // 新增副卡 1、校验限制扫码次数 判断会员与主卡是不是同一个油企下  一个主卡要么只能有共享模式的规则 要么只能有分配共享模式的规则
    // 2、根据userID + 主卡id  查询是否有副卡，没有添加；有则校验 规则id 与传参是否一致，不一致则修改，一致则异常
    // 3、一个会员在一张主卡下只会有一张副卡  只能绑定一个规则
    // 4、校验主卡余额是否满足分配金额
    @Override
    @Transactional
    public CustomerElectronicDeputyCard addElectronicDeputyCard(CustomerElectronicDeputyCard customerElectronicDeputyCard) {
        if (Objects.isNull(customerElectronicDeputyCard) || Objects.isNull(customerElectronicDeputyCard.getUserId())
                || Objects.isNull(customerElectronicDeputyCard.getRuleId())) {
            throw new CustomException("请求参数异常");
        }
        AppUserInfo appUserInfo = appUserInfoService.getById(customerElectronicDeputyCard.getUserId().intValue());
        if(Objects.isNull(appUserInfo)){
            throw new CustomException("会员不存在");
        }
        DeputyCardRule rule = deputyCardRuleMapper.selectByRuleId(customerElectronicDeputyCard.getRuleId());
        if(Objects.isNull(rule)){
            throw new CustomException("添加副卡规则不存在");
        }
        if(rule.getUsedNum() >= rule.getScanCodeNum()){
            throw new CustomException("该二维码（链接）超出使用次数");
        }
        CustomerElectronicCard customerElectronicCard = new CustomerElectronicCard();
        customerElectronicCard.setId(rule.getMainId().intValue());
        // 查询到主卡
        CustomerElectronicCard electronicCardInfo = electronicMembershipCardMapper.getElectronicCardInfo(customerElectronicCard);
        if(Objects.isNull(electronicCardInfo) || StringUtils.isEmpty(electronicCardInfo.getMobilePhone())
                || (Objects.isNull(electronicCardInfo.getStationId()) && Objects.isNull(electronicCardInfo.getStationId2()))){
            throw new CustomException("电子卡主卡不存在");
        }

        // 查询主卡用户 主卡用户和副卡用户不能一致
        if(Objects.nonNull(electronicCardInfo.getStationId())){
            AppUserInfo userInfo = new AppUserInfo();
            userInfo.setMobilePhone(electronicCardInfo.getMobilePhone());
            userInfo.setStationId(electronicCardInfo.getStationId());
            AppUserInfo appUser = appUserInfoService.getAppUserInfo(userInfo);
            if(Objects.isNull(appUser) || Objects.isNull(appUser.getUserId())){
                throw new CustomException("主卡会员不存在");
            }
            if(Objects.equals(customerElectronicDeputyCard.getUserId().intValue(),appUser.getUserId())){
                throw new CustomException("此二维码属于该会员，不能生成自己的主卡的副卡");
            }
        } else {
            Station station = stationMapper.selectStationById(electronicCardInfo.getStationId2());
            if(Objects.isNull(station) || Objects.isNull(station.getEnterpriseId())){
                throw new CustomException("主卡会员所在油站不存在");
            }
            AppUserInfo userInfo = new AppUserInfo();
            userInfo.setMobilePhone(electronicCardInfo.getMobilePhone());
            userInfo.setStationId(station.getEnterpriseId());
            AppUserInfo appUser = appUserInfoService.getAppUserInfo(userInfo);
            if(Objects.isNull(appUser) || Objects.isNull(appUser.getUserId())){
                throw new CustomException("主卡会员不存在");
            }
            if(Objects.equals(customerElectronicDeputyCard.getUserId().intValue(),appUser.getUserId())){
                throw new CustomException("此二维码属于该会员，不能生成自己的主卡的副卡");
            }
        }

        // 判断会员与主卡是不是同一个油企下
        if(Objects.nonNull(electronicCardInfo.getStationId())){
           if(!Objects.equals(appUserInfo.getStationId(),electronicCardInfo.getStationId())){
               throw new CustomException("请不要绑定其他油企电子卡");
           }

        } else if(Objects.nonNull(electronicCardInfo.getStationId2())){
            Station station = stationMapper.selectStationById(electronicCardInfo.getStationId2());
            if(!Objects.equals(appUserInfo.getStationId(),station.getEnterpriseId())){
                throw new CustomException("请不要绑定其他油企电子卡");
            }
        }

        //校验分配模式一下是否分配
        if("2".equals(rule.getCardRuleType())){
            if(electronicCardInfo.getAmt().compareTo(rule.getShareAmt()) < 0){
                throw new CustomException("主卡余额不足");
            }
        }


        CustomerElectronicDeputyCard req = new CustomerElectronicDeputyCard();
        req.setUserId(customerElectronicDeputyCard.getUserId());
//        req.setRuleId(customerElectronicDeputyCard.getRuleId());
        req.setMainId(electronicCardInfo.getId().longValue());
        // 一个用户 在一张主卡下只有一张副卡
        CustomerElectronicDeputyCard deputyCard = electronicDeputyCardMapper.selectElectronicDeputyCard(req);
        if(Objects.nonNull(deputyCard) && Objects.nonNull(deputyCard.getRuleId())){
            throw new CustomException("该会员已经加入该主卡，请勿重复加入");
        }

        // 会员在该主卡下没有副卡 新增
        if(Objects.isNull(deputyCard)){
            // 更新规则使用次数
            DeputyCardRule usedNum = new DeputyCardRule();
            usedNum.setRuleId(rule.getRuleId());
            usedNum.setUsedNum(rule.getUsedNum() + 1);
            deputyCardRuleMapper.updateByPrimaryKeySelective(usedNum);

            CustomerElectronicDeputyCard card = getCard(rule, appUserInfo, electronicCardInfo);
            log.info("新增副卡addElectronicDeputyCard:[{}]", JSONObject.toJSONString(card));
            int i = electronicDeputyCardMapper.insertSelective(card);
            if(i <= 0){
                throw new CustomException("新增副卡失败");
            }
            // 新增分配记录
            saveShareRecoveryRecord(card, rule);
            card.setMainMobilePhone(electronicCardInfo.getMobilePhone());
            return card;
        }

//        CustomerElectronicDeputyCard deputyCard = customerElectronicDeputyCards.get(0);
//        if(Objects.equals(deputyCard.getRuleId(), customerElectronicDeputyCard.getRuleId())){
//            throw new CustomException("当前已加入该卡副卡,请求主卡回收后充值");
//        }
//        // 查询旧的添加电子卡规则
//        DeputyCardRule oldDeputyCardRule = deputyCardRuleMapper.selectByRuleId(deputyCard.getRuleId());
//        if(!oldDeputyCardRule.getCardRuleType().equals(rule.getCardRuleType())){
//            throw new CustomException("该副卡的添加副卡规则类型与二维码规则类型不一致！");
//        }

        // 该卡是回收的副卡 可以重新绑定规则
        // 更新规则使用次数
        DeputyCardRule usedNum = new DeputyCardRule();
        usedNum.setRuleId(rule.getRuleId());
        usedNum.setUsedNum(rule.getUsedNum() + 1);
        deputyCardRuleMapper.updateByPrimaryKeySelective(usedNum);

        // 更新副卡余额  重新绑定规则
        CustomerElectronicDeputyCard updateCard = new CustomerElectronicDeputyCard();
        updateCard.setId(deputyCard.getId());
        updateCard.setRuleId(rule.getRuleId());
//        if(deputyCard.getAmt().compareTo(rule.getShareAmt()) < 0){
//            updateCard.setAmt(rule.getShareAmt());
//        }
        updateCard.setAmt(rule.getShareAmt());
        updateCard.setUpdateTime(new Date());
        int i = electronicDeputyCardMapper.updateByPrimaryKeySelective(updateCard);
        if(i <= 0){
            throw new CustomException("新增副卡失败");
        }
        updateCard.setMainMobilePhone(electronicCardInfo.getMobilePhone());
        // 新增分配记录
        saveShareRecoveryRecord(updateCard, rule);
        return updateCard;
    }

    private CustomerElectronicDeputyCard getCard(DeputyCardRule deputyCardRule,AppUserInfo appUserInfo,CustomerElectronicCard electronicCardInfo){
        CustomerElectronicDeputyCard newCard = new CustomerElectronicDeputyCard();
        newCard.setMainId(deputyCardRule.getMainId());
        newCard.setUserId(appUserInfo.getUserId().longValue());
        newCard.setRuleId(deputyCardRule.getRuleId());
        newCard.setCustomerNo(appUserInfo.getMemberCode());
        newCard.setCustomerName(appUserInfo.getBlogNickName());
        newCard.setMobilePhone(appUserInfo.getMobilePhone());
        newCard.setAmt(deputyCardRule.getShareAmt());
        newCard.setCardOilsType(electronicCardInfo.getCardOilsType());
        newCard.setStationId(electronicCardInfo.getStationId());
        newCard.setStationName(electronicCardInfo.getStationName());
        newCard.setStationId2(electronicCardInfo.getStationId2());

       return newCard;
    }

    private void saveShareRecoveryRecord(CustomerElectronicDeputyCard card, DeputyCardRule rule){
        ShareRecoveryRecord shareRecoveryRecord = new ShareRecoveryRecord();
        shareRecoveryRecord.setMainId(card.getMainId());
        shareRecoveryRecord.setDeputyId(card.getId());
        shareRecoveryRecord.setMobilePhone(card.getMobilePhone());
        shareRecoveryRecord.setOperationType("1");
        shareRecoveryRecord.setCardRuleType(rule.getCardRuleType());
        if("2".equals(rule.getCardRuleType())){
            shareRecoveryRecord.setEffectiveType(rule.getEffectiveType());
            shareRecoveryRecord.setOperationAmt(rule.getShareAmt());
            shareRecoveryRecord.setBalance(rule.getShareAmt());
        }
        shareRecoveryRecordMapper.insertSelective(shareRecoveryRecord);
    }

    @Override
    public CustomerElectronicDeputyCard selectElectronicDeputyCardById(Long id) {
        return electronicDeputyCardMapper.selectElectronicDeputyCardById(id);
    }

    @Override
    public int updateCustomerElectronicCard(CustomerElectronicDeputyCard customerElectronicDeputyCard) {
        return electronicDeputyCardMapper.updateByPrimaryKeySelective(customerElectronicDeputyCard);
    }

    @Override
    public int deleteElectronicDeputyCard(Long id) {
        CustomerElectronicDeputyCard customerElectronicDeputyCard = new CustomerElectronicDeputyCard();
        customerElectronicDeputyCard.setId(id);
        customerElectronicDeputyCard.setIsDeleted("1");
        customerElectronicDeputyCard.setUpdateTime(new Date());
        return electronicDeputyCardMapper.updateByPrimaryKeySelective(customerElectronicDeputyCard);
    }

    @Override
    public int recoveryElectronicDeputyCard(Long id) {
        CustomerElectronicDeputyCard customerElectronicDeputyCard = new CustomerElectronicDeputyCard();
        customerElectronicDeputyCard.setId(id);
        customerElectronicDeputyCard.setUpdateTime(new Date());
        customerElectronicDeputyCard.setAmt(BigDecimal.ZERO);
        // 副卡回收记录
        recoveryRecord(customerElectronicDeputyCard);
        return electronicDeputyCardMapper.updateById(customerElectronicDeputyCard);
    }

    private void recoveryRecord(CustomerElectronicDeputyCard customerElectronicDeputyCard){
        CustomerElectronicDeputyCard card = electronicDeputyCardMapper.selectElectronicDeputyCardById(customerElectronicDeputyCard.getId());
        if(Objects.isNull(card) || Objects.isNull(card.getRuleId())){
            throw new CustomException("该副卡已经被回收");
        }
        ShareRecoveryRecord shareRecoveryRecord = new ShareRecoveryRecord();
        shareRecoveryRecord.setMainId(card.getMainId());
        shareRecoveryRecord.setDeputyId(card.getId());
        shareRecoveryRecord.setMobilePhone(card.getMobilePhone());
        shareRecoveryRecord.setOperationType("2");

        DeputyCardRule rule = deputyCardRuleMapper.selectByRuleId(card.getRuleId());
        shareRecoveryRecord.setCardRuleType(rule.getCardRuleType());
        if("2".equals(rule.getCardRuleType())){
            shareRecoveryRecord.setEffectiveType(rule.getEffectiveType());
            shareRecoveryRecord.setOperationAmt(card.getAmt());
            shareRecoveryRecord.setBalance(BigDecimal.ZERO);
        }
        shareRecoveryRecordMapper.insertSelective(shareRecoveryRecord);
    }

    @Override
    public ExchangeCardVo exchangeCard(ExchangeCardVo exchangeCardVo) {
        PayOrder req = new PayOrder();
        req.setOrderNo(exchangeCardVo.getOrderNo());
        req.setStatus("0");
        PayOrder payOrder = payOrderMapper.selectPayOrderByStatus(req);
        if(Objects.isNull(payOrder)){
            throw new CustomException("订单不存在，" + exchangeCardVo.getOrderNo());
        }
        exchangeCardVo.setIsUsedCard("0");
        exchangeCardVo.setGenericIsUsedCard("0");

        if(StringUtils.isEmpty(payOrder.getOilName())){
            throw new CustomException("订单未关联油品，" + exchangeCardVo.getOrderNo());
        }
        Oil oil = oilMapper.selectOilById(Long.valueOf(payOrder.getOilName()));
        if(Objects.isNull(oil) || StringUtils.isEmpty(oil.getCardType())){
            throw new CustomException("油品不存在未关联卡类型，" + payOrder.getOilName());
        }

        AppUserInfo userInfo = appUserInfoService.getById(exchangeCardVo.getUserId().intValue());
        if(Objects.isNull(userInfo)){
            if(Objects.isNull(exchangeCardVo.getStationId()) || StringUtils.isEmpty(exchangeCardVo.getMobilePhone())){
                throw new CustomException("找不到会员，" + exchangeCardVo.getOrderNo());
            }
            Station station = stationMapper.selectStationById(exchangeCardVo.getStationId());
            AppUserInfo userReq = new AppUserInfo();
            userReq.setStationId(station.getEnterpriseId());
            userReq.setMobilePhone(exchangeCardVo.getMobilePhone());
            AppUserInfo appUserInfo = appUserInfoService.getAppUserInfo(userReq);
            if(Objects.isNull(appUserInfo)){
                throw new CustomException("找不到会员，" + exchangeCardVo.getOrderNo());
            } else {
                exchangeCardVo.setUserId(appUserInfo.getUserId().longValue());
            }
        } else {
            exchangeCardVo.setUserId(userInfo.getUserId().longValue());
        }

        // 判断多站单站
        Station station = stationMapper.selectStationById(payOrder.getStationId());
        Enterprise enterprise = enterpriseService.selectEnterpriseInfo(station.getEnterpriseId());
        boolean cardUseForAll = StringUtils.equals("1",enterprise.getCardUseRangeFlag());

        //储值卡  当前储值卡余额不够 使用副卡
        if(exchangeCardVo.getBalance().compareTo(BigDecimal.valueOf(payOrder.getAmt())) <0 ){
            // 查询是否有满足条件的副卡
            CustomerElectronicDeputyCard reqCard = new CustomerElectronicDeputyCard();
            reqCard.setUserId(exchangeCardVo.getUserId());
            reqCard.setCardOilsType(oil.getCardType());
            reqCard.setAmt(BigDecimal.valueOf(payOrder.getAmt()));
            if(cardUseForAll){
                reqCard.setStationId(station.getEnterpriseId());
            } else{
                reqCard.setStationId2(station.getStationId());
            }
            CustomerElectronicDeputyCard electronicDeputyCard = electronicDeputyCardMapper.selectElectronicDeputyCardByUserId(reqCard);
            if(Objects.nonNull(electronicDeputyCard)){
                exchangeCardVo.setDeputyCard(electronicDeputyCard);
                exchangeCardVo.setIsUsedCard("1");
                if("1".equals(electronicDeputyCard.getCardRuleType())){
                    exchangeCardVo.setBalance(electronicDeputyCard.getMainCardAmt());
                } else if("2".equals(electronicDeputyCard.getCardRuleType())){
                    exchangeCardVo.setBalance(electronicDeputyCard.getAmt());
                }
            }

        }

        //通用卡 当前通用卡余额不够 使用副卡
        if(exchangeCardVo.getGenericBalance().compareTo(BigDecimal.valueOf(payOrder.getAmt())) <=0 ){
            // 查询是否有满足条件的副卡
            CustomerElectronicDeputyCard reqCard = new CustomerElectronicDeputyCard();
            reqCard.setUserId(exchangeCardVo.getUserId());
            reqCard.setCardOilsType("0");
            reqCard.setAmt(BigDecimal.valueOf(payOrder.getAmt()));
            if(cardUseForAll){
                reqCard.setStationId(station.getEnterpriseId());
            } else{
                reqCard.setStationId2(station.getStationId());
            }
            CustomerElectronicDeputyCard electronicDeputyCard = electronicDeputyCardMapper.selectElectronicDeputyCardByUserId(reqCard);
            if(Objects.nonNull(electronicDeputyCard)){
                exchangeCardVo.setGenericDeputyCard(electronicDeputyCard);
                exchangeCardVo.setGenericIsUsedCard("1");
                if("1".equals(electronicDeputyCard.getCardRuleType())){
                    exchangeCardVo.setGenericBalance(electronicDeputyCard.getMainCardAmt());
                } else if("2".equals(electronicDeputyCard.getCardRuleType())){
                    exchangeCardVo.setGenericBalance(electronicDeputyCard.getAmt());
                }
            }
        }

        // 如果使用电子卡副卡创建订单信息的参数
        if("1".equals(exchangeCardVo.getIsUsedCard()) || "1".equals(exchangeCardVo.getGenericIsUsedCard())){
            PayOrderRequest payOrderRequest = new PayOrderRequest();
            payOrderRequest.setAppType(payOrder.getAppType());
            payOrderRequest.setGunName(payOrder.getOilGun());
            payOrderRequest.setReceivableAmt(payOrder.getReceivableAmt().toString());
            payOrderRequest.setUtmSource(payOrder.getUtmSource());
            payOrderRequest.setStationId(payOrder.getStationId());

            CustomerElectronicDeputyCard card = new CustomerElectronicDeputyCard();
            if(Objects.nonNull(exchangeCardVo.getDeputyCard())){
                card = exchangeCardVo.getDeputyCard();
            } else {
                card = exchangeCardVo.getGenericDeputyCard();
            }

            CustomerElectronicCard customerElectronicCard = new CustomerElectronicCard();
            customerElectronicCard.setId(card.getMainId().intValue());
            CustomerElectronicCard electronicCardInfo = electronicMembershipCardMapper.getElectronicCardInfo(customerElectronicCard);
            if(Objects.isNull(electronicCardInfo) || StringUtils.isEmpty(electronicCardInfo.getMobilePhone())){
                throw new CustomException("电子卡副卡的主卡不存在," + card.getMainId());
            }

            if(Objects.nonNull(electronicCardInfo.getStationId())){

                getAppUserInfo(electronicCardInfo.getMobilePhone(),electronicCardInfo.getStationId(),payOrderRequest);
            } else if(Objects.nonNull(electronicCardInfo.getStationId2())){

                Station sta = stationMapper.selectStationById(electronicCardInfo.getStationId2());
                getAppUserInfo(electronicCardInfo.getMobilePhone(),sta.getEnterpriseId(),payOrderRequest);
            }
            exchangeCardVo.setPayOrderRequest(payOrderRequest);

        }

        return exchangeCardVo;
    }

    private void getAppUserInfo(String mobilePhone, Integer stationId,PayOrderRequest payOrderRequest){
        AppUserInfo reqUser = new AppUserInfo();
        reqUser.setMobilePhone(mobilePhone);
        reqUser.setStationId(stationId);
        AppUserInfo appUserInfo = appUserInfoService.getAppUserInfo(reqUser);
        if(Objects.isNull(appUserInfo)){
            throw new CustomException("电子卡副卡的主卡会员不存在");
        }

        payOrderRequest.setMobilePhone(appUserInfo.getMobilePhone());
        payOrderRequest.setOpenId(appUserInfo.getBlogOpenid());
        payOrderRequest.setUserId(appUserInfo.getUserId().toString());
    }


}
